package com.isoft.airportsystem.controller;

import com.isoft.airportsystem.entity.flight.Airport;
import com.isoft.airportsystem.service.flight.AirportService;
import com.isoft.airportsystem.util.PageResult;
import com.isoft.airportsystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/airport")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:3000", "http://localhost:5173"}, allowCredentials = "true")
public class AirportController {

    @Autowired
    private AirportService airportService;

    @GetMapping("/all")
    public Result<List<Airport>> getAllAirports() {
        return Result.success(airportService.getAllAirports());
    }

    @GetMapping("/{code}")
    public Result<Airport> getByCode(@PathVariable String code) {
        return Result.success(airportService.getAirportByCode(code));
    }

    @GetMapping("/list")
    public Result<List<Airport>> queryAirports(Airport airport) {
        return Result.success(airportService.queryAirports(airport));
    }

    @GetMapping("/page")
    public Result<PageResult<Airport>> queryAirportPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            Airport airport) {
        return Result.success(airportService.queryAirportPage(pageNum, pageSize, airport));
    }

    @PostMapping
    public Result<Void> addAirport(@RequestBody Airport airport) {
        boolean success = airportService.addAirport(airport);
        return success ? Result.success() : Result.fail("新增失败");
    }

    @PutMapping
    public Result<Void> updateAirport(@RequestBody Airport airport) {
        boolean success = airportService.updateAirport(airport);
        return success ? Result.success() : Result.fail("修改失败");
    }

    @DeleteMapping("/{airportCode}")
    public Result<Void> deleteAirport(@PathVariable String airportCode) {
        boolean success = airportService.deleteAirport(airportCode);
        return success ? Result.success() : Result.fail("删除失败");
    }
}
